var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
		paypwd:'',
    hiddenmodalput: true,
		orderinfo: [],
	},
	_onLoad: function () {
		var that = this;
		$.get('wxpay/formpay', { orderid: that.options.orderid }, function (res) {
			if (res.status == 0) {
				$.error(res.msg);
			} else {
				that.setData(res)
			}
		})
	},
	getpwd:function(e){
		var paypwd = e.detail.value;
		this.setData({
			paypwd: paypwd
		});
	},
	cancel: function () {
		this.setData({
			hiddenmodalput: true
		});
	},
	modalinput: function () {
		this.setData({
			hiddenmodalput: !this.data.hiddenmodalput
		})
	},
	topay: function (e) {
		var that = this
		//var typeid = e.detail.value.paytype;
    var typeid = e.currentTarget.dataset.typeid;
		var orderid = this.data.orderinfo.id
		if (typeid == 2) {
			that.setData({hiddenmodalput:true})
			$.confirm('确定用'+(that.data.textset['余额'] || '余额')+'支付吗?', function () {
				$.post('wxpay/formpay', {orderid: orderid,typeid: typeid,paypwd:that.data.paypwd}, function (data) {
					if (data.status == 0) {
						$.error(data.msg);
						return;
					}
					if (data.status == 2) { //无需付款
						$.success(data.msg);
						setTimeout(function () {
							$.goto(that.options.tourl);
						}, 1000);
						return;
					}
				}, '提交中');
			})
		} else {
			$.post('wxpay/formpay', {orderid: orderid,typeid: typeid}, function (data) {
				if (data.status == 0) {
					$.error(data.msg);
					return;
				}
				if (data.status == 2) { //无需付款
					$.success(data.msg);
					setTimeout(function () {
						$.goto(that.options.tourl);
					}, 1000);
					return;
				}
				var opt = data.data;
				if($.platform == 'wx'){
					wx.requestPayment({
						'timeStamp': opt.timeStamp,
						'nonceStr': opt.nonceStr,
						'package': opt.package,
						'signType': 'MD5',
						'paySign': opt.paySign,
						'success': function (res) {
							$.success('付款完成');
							setTimeout(function () {
								$.goto(that.options.tourl);
							}, 1000)
						},
						'fail': function (res) { }
					})
				}else{
					tt.pay({
						orderInfo: opt,
						service: 3,
						getOrderStatus:function(res){
							var out_order_no = res;
							return new Promise(function(resolve) {
									resolve({ code: 9 });//0：支付成功 1：支付超时 2：支付失败 3：支付关闭 9：订单状态未知/未支付
							});
						},
						success:function(res) {
							if (res.code == 0) { //支付成功
								$.success('付款完成');
								setTimeout(function () {
									$.goto(that.options.tourl);
								}, 1000)
							}
							if (res.code == 9) { //支付状态未知
								//$.success('付款完成');
								setTimeout(function () {
									$.goto(that.options.tourl);
								}, 1000)
							}
						},
						fail:function(res){console.log('fail');}
					});
				}
			}, '提交中');
		}
	}
}))